﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using BbsbWebService.Classes;

namespace BbsbWsGui
{
    public class ChildDescriptionFromBaseItemConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var item = value as ControllableItem;
            if (item != null)
            {
                return GetGroupsDetails(item);
            }
            var group = value as Group;
            if (group != null)
            {
                return GetItemDetails(group);
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion

        private object GetItemDetails(Group group)
        {
            StringBuilder retVal = new StringBuilder();

            retVal.AppendLine("Items: ");
            retVal.Append("  ");
            retVal.Append(group.Items.Join(", ", o => o.Name));

            return retVal.ToString();
        }

        private string GetGroupsDetails(ControllableItem item)
        {
            StringBuilder retVal = new StringBuilder();

            retVal.AppendLine(" Groups: ");
            retVal.Append("  ");
            
            var groups = item.Groups.GetUniquePropertyList(o => o.Type);
            retVal.Append(groups.Join("\r\n  ", type =>
            {
                var typeGroups = item.Groups.Where(o => o.Type == type);
                string thing = typeGroups.Join(", ", o => o.Name);
                return string.Format("{0}: {1}", type.ToString().ToUpper(), thing);
            }));

            return retVal.ToString();
        }
    }
}
